package view;

import model.CustomArray;


/**
 * GameBoundary is the boundary that handles most interaction with the player
 * @author Group 20
 */

public class GameBoundary 
{
	/**
	 * The car color choices
	 */
	String[] colorNames = {"Blue", "Green", "Red" , "Yellow", "White", "Black"};
		
	/**
	 * Show a message of the winner
	 * @param winner: winning players player number
	 */
	public void printWinner(int winner)
	{
		Input.showMessage("Game over. Player " + winner + " won!\n");
	}
	
	/**
	 * Returns the users choice
	 * @param possibleActionTitles: array of strings
	 * @param playerName: players name
	 * @return a string with the users choice
	 */
	public String selectAction(String[] possibleActionTitles, String playerName)
	{
		int buttonChoice = Input.getPressedButton(playerName + ": Please select", 1, possibleActionTitles);
		return possibleActionTitles[buttonChoice];
	}
	
	/**
	 * Determines the users choice when he has to select a tax.
	 * @param playerBalance: value for the players balance
	 * @return
	 */
	public String selectTax(int playerBalance)
	{
		String buttons[] = new String[2];
		buttons[0] = "4000,-";
		buttons[1] = "10%";
		
		int buttonChoice = Input.getPressedButton("Pay tax! Please select: ", 1,buttons);
		
		return buttons[buttonChoice];
		
	}
	
	/**
	 * Returns the action the user does on his turn
	 * @param playerName: players name
	 * @param hasPawnableFields: boolean value
	 * @param hasBuildableFields: boolean value
	 * @param hasSellableHouses: boolean value
	 * @param hasUnpawnableFields: boolean value
	 * @return a string of the action
	 */
	public String selectAction(String playerName, boolean hasPawnableFields, boolean hasBuildableFields, boolean hasSellableHouses, boolean hasUnpawnableFields)
	{
		CustomArray<String> actionTitlesTemp = new CustomArray<String>();
		
		if(hasPawnableFields)
			actionTitlesTemp.add("Pawn field");
		
		if (hasBuildableFields)
			actionTitlesTemp.add("Buy house");
		
		if (hasSellableHouses)
			actionTitlesTemp.add("Sell house");
		
		if (hasUnpawnableFields)
			actionTitlesTemp.add("Unpawn field");
		
		actionTitlesTemp.add("End turn");
		
		String actionTitles[] = actionTitlesTemp.returnStringArray();
		int buttonChoice = Input.getPressedButton(playerName + ": Please select", 1, actionTitles);
		return actionTitles[buttonChoice];
	}
	
	/**
	 * Returns the users choice when he is about to go bankrupt
	 * @param playerName: players name
	 * @param pawnField: boolean value
	 * @param sellHouse: boolean value
	 * @return a string of the choice
	 */
	public String selectAction(String playerName, boolean pawnField, boolean sellHouse)
	{
		CustomArray<String> actionTitlesTemp = new CustomArray<String>();
		
		if(pawnField)
			actionTitlesTemp.add("Pawn field");
		
		if (sellHouse)
			actionTitlesTemp.add("Sell house");
		
		if (!pawnField && !sellHouse)
			actionTitlesTemp.add("I'm out!");
		
		
		String actionTitles[] = actionTitlesTemp.returnStringArray();
		int buttonChoice = Input.getPressedButton(playerName + ": You're out of cash. Options: ", 1, actionTitles);
		return actionTitles[buttonChoice];
	}
	
	
	/**
	 * Returns the users choice when he has to build a house
	 * @param fieldNames: array of strings with field names
	 * @param playerName: players name
	 * @return players choice
	 */
	public String selectFieldToBuildOn(String[] fieldNames, String playerName)
	{
		String fieldName = Input.getSelection(playerName + ": On which street do you want a house built?", 1, fieldNames);
	
		if (fieldName != null)
			return fieldName.substring(0, fieldName.indexOf(" ("));
		
		else
			return fieldName;
	}
	
	/**
	 * Returns the users choice when he wants to sell a house.
	 * @param fieldNames: array of strings with field names
	 * @param playerName: players name
	 * @return users choice
	 */
	public String selectFieldToSellHouseOn(String[] fieldNames, String playerName)
	{
		String fieldName = Input.getSelection(playerName + ": From which street do you want to sell a house?", 1, fieldNames);
		return fieldName;
	}
	
	/**
	 * Returns the users choice when he wants to pawn a field.
	 * @param fieldNames: array of strings with field names
	 * @param playerName: players name
	 * @return users choice
	 */
	public String selectFieldToPawn(String[] fieldNames, String playerName)
	{
		String fieldName = Input.getSelection(playerName + ": Which field do you wish to pawn?", 1, fieldNames);
		return fieldName;
	}
	
	/**
	 * Returns the users choice when he wants to unpawn a field.
	 * @param fieldNames: array of strings with field names
	 * @param playerName: players name
	 * @return user choice
	 */
	public String selectFieldToUnPawn(String[] fieldNames, String playerName)
	{
		String fieldName = Input.getSelection(playerName + ": Which field do you wish to unpawn?", 1, fieldNames);
		return fieldName;
	}
	
	/**
	 * Returns the users choice when he starts his turn.
	 * @param fieldNames: array of strings with field names
	 * @param playerName: players name
	 * @return users choice
	 */
	public int selection(int playerNumber, String playerName)
	{	
		int choice = 1;
		choice =  Input.getPressedButton("It's "+playerName+" (Player " + playerNumber+ ")'s turn", 1, "Roll", "Exit game");
		return choice;
	}
	
	/**
	 * Shows a message of how much the player has to pay or receive.
	 * @param payment: payment to be received or paid
	 * @param playerName: players name
	 */
	public void printPayment(int payment, String playerName)
	{
		if (payment > 0)
			Input.showMessage(playerName + ": You received " + payment + "kr");

		else if (payment < 0)
			Input.showMessage(playerName + ": You have to pay " + -payment + "kr");
	}
	
	/**
	 * Shows a message of the field the player landed on.
	 * @param fieldName: Fields name
	 * @param playerName: Players name
	 */
	public void printFieldName(String fieldName, String playerName)
	{
		Input.showMessage(playerName + " reached " + fieldName);
	}
	
	/**
	 * Returns the choice of the player when he has the chance to buy a field.
	 * @param price: Field price
	 * @param fieldName: Field Name
	 * @param playerName: Players name
	 * @return users choice
	 */
	public boolean wantToBuy(int price, String fieldName, String playerName)
	{	
		return Input.getYesNo("Does " + playerName + " want to buy "+ fieldName +"? (" + price + "kr.)");
	}
		
	/**
	 * Returns the number of players when the game starts
	 * @return amount of players
	 */
	public int getNumOfPlayers()
	{
		String numOfPlayers = "";
		
		numOfPlayers = Input.getSelection("Select number of players", 1, "3","4","5","6");
		
		while(numOfPlayers == null)
		{
			numOfPlayers = Input.getSelection("Select number of players", 1, "3","4","5","6");
		}
		return Integer.parseInt(numOfPlayers);
	}
	
	/**
	 * Returns the name of the player
	 * @param playerNumber: players number
	 * @return Name of player
	 */
	public String getName(int playerNumber)
	{
		String name = "";

		name = Input.getString("Player " + playerNumber + ". Type in your name: ");
			
		while(name == null || name.equals(""))
		{
			name = Input.getString("Invalid name. Try again.");
		}
			
		return name;
	}
	
	/**
	 * Returns the color chosen by the user
	 * @return users color
	 */
	public int getColor()
	{
		
		int numOfavailableColors = 0;
		for (String color: colorNames)
		{
			if (!color.equals(""))
				numOfavailableColors++;
		}
		
		String[] availableColorNames = new String[numOfavailableColors];
		
		int availableColorsIndex = 0;
		for (String color: colorNames)
		{
			if (!color.equals(""))
			{
				availableColorNames[availableColorsIndex] = color;
				availableColorsIndex++;
			}
		}
		
		int buttonNo;
		buttonNo = Input.getPressedButton("Please pick a color", 1, availableColorNames);
	
		String chosenColor = availableColorNames[buttonNo];
		
		int colorCode = 0;
		for (int i = 0; i < colorNames.length; i++) 
		{
			if (colorNames[i].equals(chosenColor))
			{
				colorCode = i;
				colorNames[i] = "";
			}
		}

		return colorCode;		
	}
}